/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.ui.rich.learn.learn_view;

//Java
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamSource;

import org.apache.avalon.framework.logger.ConsoleLogger;
import org.apache.avalon.framework.logger.Logger;
import org.apache.fop.apps.Driver;
import org.apache.fop.apps.FOPException;
import org.apache.fop.messaging.MessageHandler;

import br.ufmg.dees.insane.model.disc.fem.FemModel;

/**
 * This class makes the conversion of a Model object file to a
 * PDF using JAXP (XSLT) and FOP (XSL:FO).
 */
public class ModelToPDF {
	
	LearnInterface parent;
	FemModel model;
	String drawingFile;
	
//********************************************************************************
	
	public ModelToPDF(LearnInterface parent) {
		this.parent = parent;
	}
	
//********************************************************************************
	
	public ModelToPDF(LearnInterface parent, String drawingFile) {
		this.parent = parent;
		this.drawingFile = drawingFile;
	}
	
//********************************************************************************
	
	public void setModel(FemModel model) {
		this.model = model;
	}
	
//********************************************************************************
	
    public void convertModelToPDF(FemModel model, InputStream xslt, File pdf) throws IOException, FOPException, TransformerException {
		
        //Construct driver
        Driver driver = new Driver();
        
        //Setup logger
		Logger logger = new ConsoleLogger(ConsoleLogger.LEVEL_ERROR);
        driver.setLogger(logger);
        MessageHandler.setScreenLogger(logger);
		
		//Setup Renderer (output format)
        driver.setRenderer(Driver.RENDER_PDF);
		
        //Setup output
        OutputStream out = new java.io.FileOutputStream(pdf);
        try {
            driver.setOutputStream(out);
			
            //Setup XSLT
            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer = factory.newTransformer(new StreamSource(xslt));
			
            //Setup input for XSLT transformation
            Source src = this.getSourceForModel();
			
            //Resulting SAX events (the generated FO) must be piped through to FOP
            Result res = new SAXResult(driver.getContentHandler());
			
            //Start XSLT transformation and FOP processing
            transformer.transform(src, res);
		}finally {
            out.close();
        }
    }
	
//********************************************************************************
	
    public void convertToPDF(File pdffile) {
        try {
            
            //Setup input and output
			this.convertModelToPDF(model, this.getClass().getResourceAsStream("/modelToFO.xsl"), pdffile);
           	parent.addOutputText("File \"" + pdffile.getPath() + "\" successfully exported.");
		} catch (TransformerException e) {
			System.out.println(e.getCause());
			e.printStackTrace();
			parent.addOutputText("Error while exporting PDF File.");
		} catch (Exception e) {
			e.printStackTrace();
			parent.addOutputText("Error while exporting PDF File.");
		}
    }
	
//********************************************************************************
	
	/**
     * Resturns a Source object for this object so it can be used as input for
     * a JAXP transformation.
     * @return Source The Source object
     */
    public Source getSourceForModel() {
        //return new SAXSource(new ModelXMLReader(drawingFile),
               //new ModelInputSource(model));
        return null;
    }
	
//*****************************************************************************
}

